﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.Helper;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.Msg;


namespace SdmxMl.Panels
{
    public partial class DlgHistorySnapshot : Form
    {
        private HierarchicalCodelistType _hierarchyArtefact;

        private  HierarchyType _hierarchy;
        private Dictionary<string, CodeListType> _dicoCodeList;

        public DlgHistorySnapshot(HierarchicalCodelistType hierarchyArtefact)
        {
            InitializeComponent();
            _hierarchyArtefact = hierarchyArtefact;
            _hierarchy = _hierarchyArtefact.HierarchyList[0];
            _dicoCodeList = _hierarchy.CodelistRefList.GetRefDictionary();
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            try
            {

                HclSnapShot snapshot = new HclSnapShot(_hierarchyArtefact);
                snapshot.Generate(radioButtonHistoryNb.Checked, radioButtonHierarchyCodelist.Checked, monthCalendar.SelectionStart, textBoxSubVersion.Text.Trim());
                WriteArtefact(snapshot.ArmCodelist, snapshot.ArmHierarchy, false);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if (ex.InnerException != null)
                    msg += "\n" + ex.InnerException.Message;
                MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

         #region Artefact as SDMX File

        /// <summary> Write Artefact as SDMX Document </summary>
        /// <param name="arm">the artefact container</param>
        private void WriteArtefact(ArtefactManager arm, ArtefactManager armH, bool askForFile)
        {
            XmlTextWriter xw = null;
            string fileName = string.Empty;

            try
            {
                if (askForFile)
                {
                    saveFileDialog.Filter = "Sdmx files|*.xml";
                    saveFileDialog.FileName = armH.ArtefactList[0].FullIdent + ".xml";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK &&
                        string.IsNullOrEmpty(saveFileDialog.FileName) == false)
                    {
                        fileName = saveFileDialog.FileName;
                    }
                }
                else
                {
                    // Use temp dir 
                    fileName = System.IO.Path.GetTempPath() + "SMS_SAE_temp.xml";

                }
                if (fileName.Length > 0)
                {

                    xw = new XmlTextWriter(fileName, Encoding.UTF8);
                    xw.Formatting = Formatting.Indented;
                    xw.WriteStartDocument();

                    HeaderGenerator.GetHeader().WriteXml(xw, HeaderType.ReferenceScheme.Structure);
                    arm.WriteXml(xw, null);

                    if (armH != null)
                        armH.WriteXml(xw, null);

                    xw.WriteEndDocument();
                    xw.Close();

                    if (ReferenceManager.TreeMgr != null)
                        ReferenceManager.TreeMgr.LoadSdmxArtefact(fileName, true, null);
                }
            }
            finally
            {
                if (xw != null)
                    xw.Close();
            }
        }

        #endregion // Artefact as SDMX File


    }
}
